package com.yx.backstage.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yx.backstage.converter.FileTypeConverter;
import com.yx.backstage.model.FileType;
import com.yx.backstage.model.param.FileTypeParam;
import com.yx.backstage.model.vo.FileTypeVo;
import com.yx.backstage.service.FileTypeService;
import com.yx.backstage.config.global.BizException;
import com.yx.backstage.config.global.CommonResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author YangXiong
 */
@Component
public class FileTypeManager {

    @Resource
    private FileTypeService fileTypeService;

    public Set<String> getAllKind() {
        List<FileType> fileTypes = fileTypeService.list();
        Map<String, FileType> fileTypeMap = fileTypes.stream()
                .filter(distinctByKind(FileType::getKind))
                .collect(Collectors.toMap(FileType::getKind, Function.identity()));
        return fileTypeMap.keySet();
    }

    private Predicate<? super FileType> distinctByKind(Function<? super FileType, ?> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>(32);
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

    public Set<String> getTypeListByKind(String fileType) {
        LambdaQueryWrapper<FileType> fileTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        fileTypeLambdaQueryWrapper.eq(FileType::getFileType, fileType);
        List<FileType> list = fileTypeService.list(fileTypeLambdaQueryWrapper);
        Map<String, FileType> collect = list.stream().collect(Collectors.toMap(FileType::getFileType, Function.identity()));
        return collect.keySet();
    }

    public CommonResult<List<FileTypeVo>> getPage() {
        List<FileType> records = fileTypeService.list();
        List<FileType> collect = records.stream().filter(distinctByKind(FileType::getKind))
                .collect(Collectors.toList());
        List<FileTypeVo> result = FileTypeConverter.handlerPageResult(collect);
        return CommonResult.success(result, (long) records.size());
    }

    public CommonResult<List<FileTypeVo>> getPageByKind(FileTypeParam param) {
        String kind = param.getKind();
        if (StringUtils.isEmpty(kind)) {
            throw new BizException("参数不能为空");
        }
        QueryWrapper<FileType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("kind", kind);
        List<FileType> list = fileTypeService.list(queryWrapper);
        List<FileTypeVo> fileTypeVos = FileTypeConverter.handlerPageResult(list);
        return CommonResult.success(fileTypeVos);
    }

    public Boolean saveOrUpdate(FileTypeParam param) {
        FileType fileTypeDO = FileTypeConverter.getFileTypeDO(param);
        System.out.println(fileTypeDO);
        return fileTypeService.saveOrUpdate(fileTypeDO);
    }

    public Boolean delete(FileTypeParam param) {
        Long id = param.getId();
        if(Objects.isNull(id)){
            throw new BizException("参数不能为空");
        }
        return fileTypeService.removeById(id);
    }
}
